package com.bishe.bishe.Service.ServiceImpl;

import com.bishe.bishe.Service.SpeciesService;
import com.bishe.bishe.dao.mapper.SpeciesMapper;
import com.bishe.bishe.entity.RandomUtils;
import com.bishe.bishe.entity.WxEntity;
import com.bishe.bishe.pojo.Species;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpeciesServiceImpl implements SpeciesService {
    @Autowired
    private SpeciesMapper speciesMapper;


    @Override
    public List<Species> findall() {
        return speciesMapper.findall();
    }



    public PageInfo<Species> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        List<Species> list= speciesMapper.findall();
        PageInfo<Species> PageInfo = new PageInfo<>(list);
        return PageInfo;
    }

    @Override/*模糊查询*/
    public List<Species> findList(Map<String, Object> searchMap) {
        if (searchMap!=null){
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                return  speciesMapper.findByName(searchMap.get("name").toString());
            }else {
               return null;
            }
        }else {
            return null;
        }
    }

    @Override
    public PageInfo <Species> findPage(Map <String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        if (searchMap!=null){
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                return  new PageInfo<>(speciesMapper.findByName(searchMap.get("name").toString()));
            }else {
                return null;
            }
        }else {
            return null;
        }
    }





    /*****************/
    @Value("${absoluteImgPath}")
    String absoluteImgPath;

    @Value("C://Users/siteli/WeChatProjects/miniprogram-1/images/")
    String absoluteImgPath1;
    @Value("${sonImgPath}")
    String sonImgPath;

    @Value("${server.port}")
    String port;


    @Override
    public Map<String, String> uploadImg(MultipartFile file) {
        Map<String,String> map = new HashMap<>();
        if(file.isEmpty()){
            map.put("code","500");
            map.put("msg","图片不能为空");
            return map;
        }
        String originalFilename = file.getOriginalFilename();
        //随机生成文件名
        String fileName = RandomUtils.createRandomString(10) + originalFilename;
        File dest = new File(absoluteImgPath + fileName);
        String imgUrl = sonImgPath +fileName;
        try{
            file.transferTo(dest);
            map.put("code","200");
            map.put("msg","上传成功");
            map.put("imgUrl",imgUrl);//imgUrl图片路径
            map.put("originalFilename",originalFilename);
            copyFile1(absoluteImgPath + fileName,absoluteImgPath1+fileName);
            return map;
        }catch (IllegalStateException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return map;
    }
    public static void copyFile1(String srcPath, String destPath) throws IOException {
        // 打开输入流
        FileInputStream fis = new FileInputStream(srcPath);
        // 打开输出流
        FileOutputStream fos = new FileOutputStream(destPath);

        // 读取和写入信息
        int len = 0;
        while ((len = fis.read()) != -1) {
            fos.write(len);
        }

        // 关闭流  先开后关  后开先关
        fos.close(); // 后开先关
        fis.close(); // 先开后关

    }

    @Override
    public Species findById(int speciesId) {
        return speciesMapper.finddetailById(speciesId);
    }

    @Override
    public List<Species> findByName(String speciesName) {
        return speciesMapper.findByNames(speciesName);
    }

    @Override
    public int insert(Species species) {
        return speciesMapper.insert(species);
    }

    @Override
    public int updatestate(int speciesId,String speciesState) {
        return speciesMapper.updatestate(speciesId,speciesState);
    }
    @Override
    public List<Species> findByNames(String speciesName) {
        return speciesMapper.findByName(speciesName);
    }

    @Override
    public int update(Species species) {
        return speciesMapper.update(species.getSpeciesName(),species.getSpeciesPrice(),species.getSpeciesIntroduce(),species.getTypes().getTypeName(),species.getSpeciesTotal(),species.getUnit().getUnitName(),species.getSpeciesState(),species.getSpeciesId());
    }

    @Override
    public int updateImg(Species species) {
        return speciesMapper.updateImg2(species);
    }

    @Override
    public List <String> listImg() {
        return speciesMapper.listImg();
    }


}
